package com.ylx.web.s;

import com.alibaba.fastjson.JSON;
import com.ylx.pojo.Goods;
import com.ylx.service.GoodsService;
import com.ylx.service.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {

         private GoodsService goodsService=new GoodsServiceImpl();
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1. 接收品牌数据
           BufferedReader br=request.getReader();

           String params = br.readLine();


        //转为Brand对象

           Goods goods = JSON.parseObject(params, Goods.class);

           goodsService.add(goods);
           response.getWriter().write("success");

//        //1. 接收品牌数据
//        BufferedReader br = request.getReader();
//        String params = br.readLine();//json字符串
//
//        //转为Brand对象
//        Brand brand = JSON.parseObject(params, Brand.class);
//
//        //2. 调用service添加
//        brandService.add(brand);
//
//        //3. 响应成功的标识
//        response.getWriter().write("success");
    }


    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);

    }
}
